package org.jeecg.modules.htxincailiao.patent.controller;

import java.util.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.RequestUtil;
import org.jeecg.modules.htxincailiao.patent.entity.HTPatentCategory;
import org.jeecg.modules.htxincailiao.patent.entity.HTPatentDemand;
import org.jeecg.modules.htxincailiao.patent.service.IHTPatentCategoryService;
import org.jeecg.modules.htxincailiao.patent.service.IHTPatentDemandService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.htxincailiao.shop.entity.HTShop;
import org.jeecg.modules.htxincailiao.shop.service.IHTShopService;
import org.jeecg.modules.permissions.HTRolePermission;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.oConvertUtils;

 /**
 * @Description: ht_patent_demand
 * @Author: jeecg-boot
 * @Date:   2021-11-14 16:32:55
 * @Version: V1.0
 */
@Api(tags="ht_patent_demand")
@RestController
@RequestMapping("/patent/hTPatentDemand")
@Slf4j
public class HTPatentDemandController extends JeecgController<HTPatentDemand, IHTPatentDemandService> {
	@Autowired
	private IHTPatentDemandService hTPatentDemandService;

	 @Resource
	 private IHTPatentCategoryService patentCategoryService;

	 @Resource
	 private IHTShopService shopService;



	/**
	 * 分页列表查询
	 *
	 * @param hTPatentDemand
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "ht_patent_demand-分页列表查询")
	@ApiOperation(value="ht_patent_demand-分页列表查询", notes="ht_patent_demand-分页列表查询")
	@RequestMapping(value = "/list", method = {RequestMethod.GET,RequestMethod.POST})
	public Result<?> queryPageList(HTPatentDemand hTPatentDemand,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		if (req.getMethod().equals("POST")){
			String line = RequestUtil.getPostParam(req);
			if (oConvertUtils.isNotEmpty(line)) {
				hTPatentDemand = JSONObject.parseObject(line, HTPatentDemand.class);

				pageNo = hTPatentDemand.getPageNo();
				pageSize = hTPatentDemand.getPageSize();
			}
		}

		if(oConvertUtils.isNotEmpty(hTPatentDemand) &&
				oConvertUtils.isNotEmpty(hTPatentDemand.getCategoryId()) &&
				hTPatentDemand.getCategoryId().equals("0")) {
			//全部分类
			hTPatentDemand.setCategoryId(null)	;
		}

		HTRolePermission.AddPatentDemandPermission(req, hTPatentDemand);

		QueryWrapper<HTPatentDemand> queryWrapper = QueryGenerator.initQueryWrapper(hTPatentDemand, req.getParameterMap());
		queryWrapper.orderByDesc("update_time");
		Page<HTPatentDemand> page = new Page<HTPatentDemand>(pageNo, pageSize);
		IPage<HTPatentDemand> pageList = hTPatentDemandService.page(page, queryWrapper);
		List<HTPatentDemand> records = pageList.getRecords();
		records.forEach(demand->{

			HTPatentCategory category = patentCategoryService.getById(demand.getCategoryId());
			if(oConvertUtils.isNotEmpty(category)){
				demand.setCategoryName(category.getName());
			}

			HTShop hTShop = shopService.getShopInfo(demand.getShopId());
			if(oConvertUtils.isNotEmpty(hTShop)){
				demand.setShopInfo(hTShop);
			}

		});
		return Result.OK(pageList);
	}	/**
	 * 分页列表查询
	 *
	 * @param hTPatentDemand
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "ht_patent_demand-分页列表查询")
	@ApiOperation(value="ht_patent_demand-分页列表查询", notes="ht_patent_demand-分页列表查询")
	@RequestMapping(value = "/pc-home-list", method = {RequestMethod.GET,RequestMethod.POST})
	public Result<?> queryPCHomePageList(HTPatentDemand hTPatentDemand,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		if (req.getMethod().equals("POST")){
			String line = RequestUtil.getPostParam(req);
			if (oConvertUtils.isNotEmpty(line)) {
				hTPatentDemand = JSONObject.parseObject(line, HTPatentDemand.class);

				pageNo = hTPatentDemand.getPageNo();
				pageSize = hTPatentDemand.getPageSize();
			}
		}

		if(oConvertUtils.isNotEmpty(hTPatentDemand) &&
				oConvertUtils.isNotEmpty(hTPatentDemand.getCategoryId()) &&
				hTPatentDemand.getCategoryId().equals("0")) {
			//全部分类
			hTPatentDemand.setCategoryId(null)	;
		}

		HTRolePermission.AddPatentDemandPermission(req, hTPatentDemand);

		QueryWrapper<HTPatentDemand> queryWrapper = QueryGenerator.initQueryWrapper(hTPatentDemand, req.getParameterMap());
		queryWrapper.orderByDesc("is_home_recommended").orderByDesc("update_time");

		Page<HTPatentDemand> page = new Page<HTPatentDemand>(pageNo, pageSize);
		IPage<HTPatentDemand> pageList = hTPatentDemandService.page(page, queryWrapper);
		List<HTPatentDemand> records = pageList.getRecords();
		records.forEach(demand->{

			HTPatentCategory category = patentCategoryService.getById(demand.getCategoryId());
			if(oConvertUtils.isNotEmpty(category)){
				demand.setCategoryName(category.getName());
			}

			HTShop hTShop = shopService.getShopInfo(demand.getShopId());
			if(oConvertUtils.isNotEmpty(hTShop)){
				demand.setShopInfo(hTShop);
			}

		});
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param hTPatentDemand
	 * @return
	 */
	@AutoLog(value = "ht_patent_demand-添加")
	@ApiOperation(value="ht_patent_demand-添加", notes="ht_patent_demand-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody HTPatentDemand hTPatentDemand) {
		hTPatentDemandService.save(hTPatentDemand);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param hTPatentDemand
	 * @return
	 */
	@AutoLog(value = "ht_patent_demand-编辑")
	@ApiOperation(value="ht_patent_demand-编辑", notes="ht_patent_demand-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody HTPatentDemand hTPatentDemand) {
		hTPatentDemandService.updateById(hTPatentDemand);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "ht_patent_demand-通过id删除")
	@ApiOperation(value="ht_patent_demand-通过id删除", notes="ht_patent_demand-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		hTPatentDemandService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "ht_patent_demand-批量删除")
	@ApiOperation(value="ht_patent_demand-批量删除", notes="ht_patent_demand-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.hTPatentDemandService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "ht_patent_demand-通过id查询")
	@ApiOperation(value="ht_patent_demand-通过id查询", notes="ht_patent_demand-通过id查询")
	@RequestMapping(value = "/queryById", method = {RequestMethod.GET,RequestMethod.POST})
	public Result<?> queryById(HttpServletRequest req, @RequestParam(name="id",required=false) String id) {
		if (req.getMethod().equals("POST")){
			String line = RequestUtil.getPostParam(req);
			if (oConvertUtils.isNotEmpty(line)) {
				HTPatentDemand hTPatentDemand  = JSONObject.parseObject(line, HTPatentDemand.class);
				id = hTPatentDemand.getId();
			}
		}

		HTPatentDemand hTPatentDemand = hTPatentDemandService.getById(id);
		if (oConvertUtils.isNotEmpty(hTPatentDemand) &&
				oConvertUtils.isNotEmpty(hTPatentDemand.getCategoryId())){
			HTPatentCategory category = patentCategoryService.getById(hTPatentDemand.getCategoryId());
			if (oConvertUtils.isNotEmpty(category) &&
					oConvertUtils.isNotEmpty(category.getName())){
				hTPatentDemand.setCategoryName(category.getName());
			}
		}
		if(hTPatentDemand==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(hTPatentDemand);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param hTPatentDemand
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HTPatentDemand hTPatentDemand) {
        return super.exportXls(request, hTPatentDemand, HTPatentDemand.class, "ht_patent_demand");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HTPatentDemand.class);
    }

}
